import path, { resolve } from 'path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import fs from "fs";

function getEntryPath () {
  const map = {} //最后生成的多页面配置项
  const PAGE_PATH = path.resolve(__dirname, './src/views/pages')  //指定要查询的目录
  const entryFiles = fs.readdirSync(PAGE_PATH)   //获取到指定目录下的所有文件名
  entryFiles.forEach(filePath => {   //遍历处理每个子页面的入口
    const _path = filePath.replace(/\.[^/.]+$/, "");
    map[_path] = path.resolve(__dirname,
    `src/views/pages/${_path}.html`
    )
  })
  return map
}

export default defineConfig({
  resolve: {
		alias: {
			"@": resolve(__dirname, './src'),
      '~/': resolve(__dirname, 'src'),
		}
	},
  root: 'src/views',
  base: './',
  plugins: [vue()],
  build: {
    outDir: resolve(process.cwd(), 'build'),
    // assetsDir: 'pages/assets',
    rollupOptions: {
      // 配置多页面
      input: getEntryPath(),
    },
  },
  server: {
    host: '0.0.0.0',
    //   // 是否自动在浏览器打开
    //   open: false,
    proxy: {
      '/api': {
        target: 'https://ar.foxech.com/api',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''),
      },
    },
  }
})
